# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymysql


class ShopPipeline(object):
    def __init__(self):
        self.conn = pymysql.Connection(host='47.103.14.96', port=3306, user='root', password='123456',
                                       database='xuanba')

    def process_item(self, item, spider):
        conn = self.conn
        with conn.cursor() as cursor:
            sql = f'insert into tb_shop (`name`,score, price, tel, adress, time, picture, is_wifi,longitude, ' \
                f'latitude) value ("{item["name"]}", {item["score"]}, {item["avg_price"]}, "{item["tel"]}", ' \
                f'"{item["address"]}", "{item["time"]}", "{item["picture"]}", {item["is_wifi"]}, {item["longitude"]}, ' \
                f'{item["latitude"]});'
            cursor.execute(sql)
            conn.commit()
        return item
